function [q,vli]=genq(pls,w)
    vli=zeros(gsize,3*gsize);
    q=zeros(gsize,3*gsize,3);
    for j=3*gsize-1:-1:1
        for i=1:gsize
            vli(i,j)=rw(i-1,j-1,pls(i,j),w)+vli(i+pls(i,j),j+1);
            if i==1
                for k=2:3
                    q(i,j,k)=rw(i-1,j-1,k-2,w)+vli(i+k-2,j+1);
                end
            elseif i==gsize
                 for k=1:2
                    q(i,j,k)=rw(i-1,j-1,k-2,w)+vli(i+k-2,j+1);
                end
            else
                for k=1:3
                    q(i,j,k)=rw(i-1,j-1,k-2,w)+vli(i+k-2,j+1);
                end
            end
        end
    end
endfunction
function r=evals(pls,vli)
    r=0;
    s=0;
    for t=1:3*gsize-1;
        r=r+vli(s+1,t);
        s=s+pls(s+1,t);
    end
endfunction
